package com.study.zgw.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Client {

    public static void main(String[] args) {
        //1
        //11
        try {
            SocketChannel socketChannel = SocketChannel.open();
            socketChannel.connect(new InetSocketAddress("localhost",8080));

            socketChannel.write(Charset.defaultCharset().encode("hello "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            socketChannel.read(byteBuffer);
            byteBuffer.flip();//切换读模式
            System.out.println("------收到的数据："+Charset.defaultCharset().decode(byteBuffer).toString());
            System.out.println("-----waiting-----");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
